<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use \Illuminate\Http\Request;

class MlcmsUser extends Model
{
    protected $table="mlmcms_user";
    public $timestamps=false;
    protected $fillable = [];
    protected $guarded =[];
    protected $primaryKey  = "MUID";
    /**
     * 验证登陆
     * @param Request $request
     * @return type
     */
    public static function AuthLogin(Request $request) {
        if (!$request->input('mu_number', false) || !$request->input('mu_onepwd', false)) {
            return aaa_return(0, "请正确填写用户名和密码");
        }
        $user = static::where('Mu_Number', $request->input('mu_number'))->first();
        if (!$user) {
            return aaa_return(0, "用户不存在");
        }
        if ($user->Mu_OnePwd != aaa_md5($request->input('mu_onepwd'))) {
            return aaa_return(0, "密码不正确");
        }
        return aaa_return(1, $user);
    }
    
    /**
     * 获取用户信息
     * @param type $uid
     * @return type
     */
    public static function getUserInfo($uid) {
        $user = static::where('MUID', $uid)->first();
        return $user;
    }
    
    /**
     * 修改用户信息
     * @param Request $request
     */
    public static function modifyUser(Request $request) {
        $user = $request->session()->get("Home");
        if (aaa_md5($request->Mu_TwoPwd) != $user->Mu_TwoPwd) {
            return aaa_return(0, "二级密码不正确");
        }
        $data['Mu_sex'] = $request->input('Mu_sex');
        $data['Mu_Age'] = $request->input('Mu_Age');
        $data['Mu_Mobile'] = $request->input('Mu_Mobile');
        $data['Mu_Code'] = $request->input('Mu_Code');
        $data['Mu_CardNo'] = $request->input('Mu_CardNo');
        $data['Mu_Address'] = $request->input('Mu_Address');
        $data['Mu_OpenBank'] = $request->input('Mu_OpenBank');
        $data['Mu_OpenBankNo'] = $request->input('Mu_OpenBankNo');
        $data['Mu_OpenBankName'] = $request->input('Mu_OpenBankName');
        $data['Mu_OpenBankUrl'] = $request->input('Mu_OpenBankUrl');
        static::where('MUID', $user->MUID)->update($data);
        foreach($data as $key=>$val) {
            $user->$key = $val;
        }
        return aaa_return(1, "资料修改成功");
    }
    /**
     * 修改密码
     * @param Request $request
     */
    public static function modPwd(Request $request) {
        $user = $request->session()->get("Home");
        $onepwd = $request->input("Mu_OnePwd",false);
        $twopwd = $request->input('Mu_TwoPwd',false);
        if(!$onepwd && !$twopwd) {
            return aaa_return(1, "密码没有修改");
        }
        if ($onepwd) {
            $data['Mu_OnePwd'] = aaa_md5($onepwd);
            $user->Mu_OnePwd = $data['Mu_OnePwd'];
        }
        if ($twopwd) {
            $data['Mu_TwoPwd'] = aaa_md5($twopwd);
            $user->Mu_TwoPwd = $data['Mu_TwoPwd'];
        }
        static::where("MUID", $user->MUID)->update($data);
        return aaa_return(1, "密码修改成功");
    }
    /**
     * 升级会员
     * @param Request $request
     */
    public static function upgradeUser(Request $request) {
        $user = $request->session()->get("Home");
        $dzcoin = 0;
        $upp = $request->input('Mu_BuyProduct', false);
        if (!$upp) {
            return aaa_return(0, "会员升级失败");
        }
        if ($upp == $user->Mu_BuyProduct) {
            return aaa_return(0, "操作非法");
        }
        $setmoney = MlcmsSetmoney::getByPname($upp);
        if (!$setmoney) {
            return aaa_return(0, "操作非法");
        }
        if ($user->Mu_BuyFunds < $setmoney->Mse_ProductMoney) {
            return aaa_return(0, "用户电子币账户不足".$setmoney->Mse_ProductMoney."，无法升级");
        }
        $data['Mu_BuyFunds'] = $user->Mu_BuyFunds - $setmoney->Mse_ProductMoney;
        $data['Mu_Part'] = 1;
        $data['Mu_BuyProduct'] = $setmoney->Mse_ProductName;
        $data['Mu_IsFox'] = 3;
        $data['Mu_BuyMoneys'] = $user->Mu_BuyMoneys + $setmoney->Mse_ProductMoney;
        if (static::where('MUID', $user->MUID)->update($data)) {
            //添加升级日志记录
            MlcmsUpgradelog::addLog(['Mbo_number'=>$user->Mu_Number, 'Mbo_Income'=>$setmoney->Mse_ProductMoney,'Mbo_Pay'=>0,'Mbo_Bochubi'=>0,'createtime'=>date("Y-m-d H:i:s")]);
            $user->Mu_BuyFunds -= $setmoney->Mse_ProductMoney;
            $user->Mu_Part = 1;
            $user->Mu_BuyProduct = $setmoney->Mse_ProductName;
            $user->Mu_IsFox = 3;
            $user->Mu_BuyMoneys += $setmoney->Mse_ProductMoney;
            return aaa_return(1, '升级成功');
        } else {
            return aaa_return(0, "升级失败");
        }
        
        //执行会员升级流程
        
//        select @buycount=Mse_BuyCount from MLMCMS_SetMoney where Mse_ProductName='商务中心'
//        update MLMCMS_User set Mu_BuyFunds=Mu_BuyFunds-10000,Mu_Part=1 where Mu_Number=@Mu_Number
//        update MLMCMS_User set Mu_BuyProduct='商务中心' where Mu_Number=@Mu_Number
//        update MLMCMS_User set Mu_IsFox=3,Mu_BuyMoneys=Mu_BuyMoneys+10000 where Mu_Number=@Mu_Number
//        --update MLMCMS_User set Mu_IsFox=3 where Mu_Number=@Mu_Number
//        --写入会员升级新增收入表 2017.03.18
//        insert into MLMCMS_UpGradeLog(Mbo_number,Mbo_Income,Mbo_Pay,Mbo_Bochubi,createtime)values(@Mu_Number,10000,0,0,GETDATE())
    }
    
    
    /**
     * 奖金币
     */
    public function jjcoin() {
        return round($this->Mu_AllMoney);
    }
    /**
     * 电子币
     * @return type
     */
    public function dzcoin() {
        return $this->Mu_BuyFunds;
    }
    /**
     * 复消币
     * @return type
     */
    public function fxcoin() {
        return "XXX";
    }
    /**
     * 电子币
     * @return type
     */
    public function dhcoin() {
        return "XXX";
    }
    /**
     * 是否是商务中心
     */
    public function isAgent() {
        return $this->Mu_IsAgent ? '是' : '否';
    }
}
